package com.kzd.btgame.main.mine

import android.os.Bundle
import android.text.Editable
import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.core.widget.addTextChangedListener
import com.kzd.btgame.custom.setOnClickAntiShakeListener
import com.kzd.btgame.databinding.FragmentAddressEditBinding
import com.kzd.btgame.entity.UserAddress
import com.kzd.btgame.jetpack.vm.AddressViewModel
import com.kzd.btgame.main.KEY_ADDRESS_INFO
import com.module.lemlin.owner.OwnerViewBindingFragment
import com.module.lemlin.owner.ownerViewModel

class AddressEditFragment : OwnerViewBindingFragment<FragmentAddressEditBinding>() {

    companion object {
        fun newInstance(userAddress: UserAddress? = null): AddressEditFragment {
            val bundle = Bundle().apply {
                putSerializable(KEY_ADDRESS_INFO, userAddress)
            }
            return AddressEditFragment().apply {
                arguments = bundle
            }
        }
    }

    private val mUserAddress: UserAddress by lazy {
        (arguments?.getSerializable(KEY_ADDRESS_INFO) as? UserAddress) ?: UserAddress()
    }

    private val mViewModel: AddressViewModel by lazy {
        ownerViewModel(requireActivity())
    }

    override val inflate: (LayoutInflater, ViewGroup?, Boolean) -> FragmentAddressEditBinding
        get() = FragmentAddressEditBinding::inflate

    override fun initView() {


        mUserAddress.consignee?.let {
            mViewBinding.etAddressName.text = Editable.Factory.getInstance().newEditable(it)
        }
        mUserAddress.mobile?.let {
            mViewBinding.etAddressPhone.text = Editable.Factory.getInstance().newEditable(it)
        }
        mUserAddress.uaddress?.let {
            mViewBinding.etAddressDetails.text = Editable.Factory.getInstance().newEditable(it)
        }
        mViewBinding.cbAddressIsDefault.isChecked = mUserAddress.moren == "1"

        mViewBinding.tvAddressKeep.isEnabled = !mUserAddress.consignee.isNullOrEmpty()
                && !mUserAddress.mobile.isNullOrEmpty()
                && !mUserAddress.uaddress.isNullOrEmpty()

        mViewBinding.etAddressName.addTextChangedListener {
            mViewBinding.tvAddressKeep.isEnabled = !it.isNullOrEmpty()
                    && !mViewBinding.etAddressPhone.text.isNullOrEmpty()
                    && !mViewBinding.etAddressDetails.text.isNullOrEmpty()
            mUserAddress.apply { consignee = it?.toString() }
        }
        mViewBinding.etAddressPhone.addTextChangedListener {
            mViewBinding.tvAddressKeep.isEnabled = !it.isNullOrEmpty()
                    && !mViewBinding.etAddressName.text.isNullOrEmpty()
                    && !mViewBinding.etAddressDetails.text.isNullOrEmpty()
            mUserAddress.apply { mobile = it?.toString() }
        }
        mViewBinding.etAddressDetails.addTextChangedListener {
            mViewBinding.tvAddressKeep.isEnabled = !it.isNullOrEmpty()
                    && !mViewBinding.etAddressName.text.isNullOrEmpty()
                    && !mViewBinding.etAddressPhone.text.isNullOrEmpty()
            mUserAddress.apply { uaddress = it?.toString() }
        }

        mViewBinding.cbAddressIsDefault.setOnCheckedChangeListener { _, isChecked ->
            mUserAddress.apply {
                moren = if (isChecked) "1" else "0"
            }
        }

        mViewBinding.tvAddressKeep.setOnClickAntiShakeListener {
            val id = mUserAddress.id
            val consignee = mUserAddress.consignee
                ?: return@setOnClickAntiShakeListener
            val mobile = mUserAddress.mobile
                ?: return@setOnClickAntiShakeListener
            val uaddress = mUserAddress.uaddress
                ?: return@setOnClickAntiShakeListener
            val moren = mUserAddress.moren
            mViewModel.userAddressKeep(id, consignee, mobile, uaddress, moren)
        }
    }
}